package com.coocaa.ops.admin.v1.hvi.provider;

import com.coocaa.ops.admin.base.AbstractBizImpl;
import com.coocaa.ops.admin.base.validator.InputParamsValidator;
import com.coocaa.ops.admin.v1.audit.provider.BaseAuditTaskProviderImpl;
import com.coocaa.ops.admin.v1.hvi.validator.HistoryInfoValidator;
import com.coocaa.ops.common.core.base.entity.BaseHistoryTableEntity;
import com.coocaa.ops.common.core.base.entity.BaseOriginTableEntity;
import com.coocaa.ops.common.core.base.model.ResourceHistoryInfoModel;
import com.coocaa.ops.common.core.base.mybatis.QueryTemplate;
import com.coocaa.ops.common.tool.base.InstanceUtil;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * @author bijiahao
 * @date : 2018/7/24.
 * @description 历史下拉提供者接口抽象实现
 */
public abstract class AbstractHistoryInfoProviderImpl<T extends BaseHistoryTableEntity> implements IHistoryInfoProvider {

    /**
     * 获取基础业务接口实现对象
     *
     * @return
     */
    abstract public AbstractBizImpl getAbstractBizImpl();

    @Override
    public int getHistoryCount(Integer id) {
        QueryTemplate<T> wrapper = new QueryTemplate<>();
        wrapper.eq("id", id)
                .eq("flag", 1);
        Integer count = getAbstractBizImpl().getHistoryMapper().selectCount(wrapper);
        return count == null ? 0 : count;
    }

    @Override
    public List<ResourceHistoryInfoModel> getHistoryList(Integer id, Integer limit, Integer offset) {
        List<ResourceHistoryInfoModel> historyInfoList = getAbstractBizImpl().getHistoryMapper().getHistoryList(id, limit, offset);
        //此操作似乎不需要
        //this.handleAuditNotOnline(historyInfoList, id);
        if (HistoryInfoValidator.isNeedAddV1(id, historyInfoList)) {
            ResourceHistoryInfoModel resourceHistoryV1 = getAbstractBizImpl().getHistoryMapper().queryHistoryByIdAndVersion(id, BaseAuditTaskProviderImpl.V_1);
            if (null != resourceHistoryV1) {
                historyInfoList.add(resourceHistoryV1);
            }
        }
        return historyInfoList;


    }

    private void handleAuditNotOnline(List<ResourceHistoryInfoModel> result, Integer id) {
        List<ResourceHistoryInfoModel> auditNotOnlineList = getAbstractBizImpl().getHistoryMapper().listAuditNotOnline(id);
        if (CollectionUtils.isNotEmpty(auditNotOnlineList)) {
            result.addAll(auditNotOnlineList);
        }
    }

}
